﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataLibrary
{
    public class Ctbl_DetailProduct:IData<tbl_DetailProduct>
    {
        public Ctbl_DetailProduct() { }
        public Ctbl_DetailProduct(String myconnecString) : base(myconnecString) { }

        public override List<tbl_DetailProduct> GetAll(tbl_DetailProduct item)
        {
            throw new NotImplementedException();
        }

        public override tbl_DetailProduct GetItemById(object id)
        {
            return GetAll().SingleOrDefault(p => p.Id == Convert.ToInt32(id));
        }

        public override tbl_DetailProduct GetItem(System.Data.IDataReader dr){
            tbl_DetailProduct obj = new tbl_DetailProduct();
            obj.Description = Convert.ToString(dr[tbl_DetailProduct.c_Description]);
            obj.Id = Convert.ToInt32(dr[tbl_DetailProduct.c_id]);
            obj.IdProduct = Convert.ToInt32(dr[tbl_DetailProduct.c_idProduct]);
            obj.IsActive = Convert.ToInt32(dr[tbl_DetailProduct.c_isActive]);
            obj.Name = Convert.ToString(dr[tbl_DetailProduct.c_name]);
            obj.OrderId = Convert.ToInt32(dr[tbl_DetailProduct.c_orderId]);
            return obj;
        }

        public override List<tbl_DetailProduct> GetAll()
        {
            String strSQL = "select * from " + tbl_DetailProduct.b_tbl_DetailProduct + " order by " + tbl_DetailProduct.c_orderId +" desc" ;
            return GetAll(strSQL, CommandType.Text);
        }

        protected override int InsertOnSubmit(tbl_DetailProduct item, int t)
        {
            int id = -1;
            String strProName = "ProDetailProduct";
            id = connec.ExecuteNonQuery(strProName,CommandType.StoredProcedure,new SqlParameter("@flag",t),
                new SqlParameter("@"+tbl_DetailProduct.c_Description,item.Description),new SqlParameter("@"+tbl_DetailProduct.c_id,item.Id),
                new SqlParameter("@"+tbl_DetailProduct.c_idProduct,item.IdProduct),new SqlParameter("@"+tbl_DetailProduct.c_isActive,item.IsActive),
                new SqlParameter("@"+tbl_DetailProduct.c_name,item.Name),new SqlParameter("@"+tbl_DetailProduct.c_orderId,item.OrderId));

            return id;
        }

        public List<tbl_DetailProduct>GetTabDetail(int idProduct){
            return GetAll().FindAll(p=>p.IdProduct==idProduct && p.IsActive==1);
        }

    }
}
